/**============================================================================
 *
 * Author: luo1o1o1o 
 *
 * QQ: 330240995, PHONE: 17600071321
 *
 * Last modified: 2021-06-01 17:49
 *
 * Filename: user.js
 *
 * Description: user controller
 *
 ============================================================================**/
const {
    getUserInfo,
    createUser,
    deleteUser,
    updateUser,
} = require('../services/user')
const { SuccessModel, ErrorModel } = require('../model/ResModel') // 返回的格式
const doCrypto = require('../utils/cryp')
const {
    registerUserNameNotExistInfo,
    registerUserNameExistInfo,
    registerFailInfo,
    loginFailInfo,
    deleteUserFailInfo,
    changeInfoFailInfo,
    changePasswordFailInfo,
} = require('../model/ErrorInfo')

/**
 * @Author: luo1o1o1o
 * @QQ: 330240995, @PHONE: 17600071321
 * @Date: 2021-06-01 17:50
 * @desc: 用户名是否存在
 */
async function isExist(userName) {
    // 业务逻辑处理
    // 调用services
    const userInfo = await getUserInfo(userName)
    if (userInfo) {
        // 已存在
        return new SuccessModel(userInfo)
    } else {
        // 不存在
        return new ErrorModel(registerUserNameNotExistInfo)
    }
    // 统一返回格式
}

/**
 * @Author: luo1o1o1o
 * @QQ: 330240995, @PHONE: 17600071321
 * @Date: 2021-06-07 10:54
 * @desc: 注册
 */
async function register({ userName, password, gender }) {
    const userInfo = await getUserInfo(userName)
    if (userInfo) {
        // 用户名已存在
        return new ErrorModel(registerUserNameExistInfo)
    }
    // 注册
    try {
        await createUser({
            userName,
            password: doCrypto(password),
            gender,
        })
        return new SuccessModel()
    } catch (ex) {
        console.error(ex.message, ex.stack)
        return new ErrorModel(registerFailInfo)
    }
}

async function login(ctx, userName, password) {
    // 登录成功 ctx.session.userInfo = xxxx
    const userInfo = await getUserInfo(userName, password)
    if (!userInfo) {
        return new ErrorModel(loginFailInfo)
    }

    // 登录成功
    if (ctx.session.userInfo == null) {
        ctx.session.userInfo = userInfo
    }
    return new SuccessModel()
}

/**
 * @Author: luo1o1o1o
 * @QQ: 330240995, @PHONE: 17600071321
 * @Date: 2021-06-18 00:46
 * @desc: 删除当前用户
 */
async function deleteCurUser(userName) {
    // services
    const result = await deleteUser(userName)
    if (result) {
        // 成功
        return new SuccessModel(deleteUserFailInfo)
    }
    // 失败
    return new ErrorModel()
}

/**
 * @Author: luo1o1o1o
 * @Date: 2021-06-27 14:03
 * @desc: 修改用户
 * @param: {Object} ctx ctx
 * @param: {string} nickName 昵称
 * @param: {string} city 城市
 * @param: {string} picture 头像
 */
async function changeInfo(ctx, { nickName, city, picture }) {
    const { userName } = ctx.session.userInfo
    if (!nickName) {
        nickName = userName
    }

    const result = await updateUser(
        {
            newNickName: nickName,
            newCity: city,
            newPicture: picture,
        },
        {
            userName,
        }
    )

    if (result) {
        Object.assign(ctx.session.userInfo, {
            nickName,
            city,
            picture,
        })

        // 返回
        return new SuccessModel()
    }

    // 失败
    return new ErrorModel(changeInfoFailInfo)
}

/**
 * @Author: luo1o1o1o
 * @Date: 2021-06-28 11:29
 * @desc: 修改密码
 * @param: {string} userName 用户名
 * @param: {string} password 当前密码
 * @param: {string} newPassword 新密码
 */
async function changePassword(userName, password, newPassword) {
    const result = await updateUser(
        {
            newPassword: doCrypto(newPassword),
        },
        {
            userName,
            password: doCrypto(password),
        }
    )
    if (result) {
        // 成功
        return new SuccessModel()
    }
    // 失败
    return new ErrorModel(changePasswordFailInfo)
}

/**
 * @Author: luo1o1o1o
 * @Date: 2021-06-30 11:48
 * @desc: 退出登录
 * @param: {Object} ctx ctx
 */
async function logout(ctx) {
    delete ctx.session.userInfo
    return new SuccessModel()
}

module.exports = {
    isExist,
    register,
    login,
    deleteCurUser,
    changeInfo,
    changePassword,
    logout,
}
